package com.zerosoft.shuguang;

import com.zerosoft.shuguang.config.MqttProperties;
import com.zerosoft.shuguang.config.SerialPortConfig;
import com.zerosoft.shuguang.config.SerialPortProperties;
import com.zerosoft.shuguang.serial.SerialPortManager;
import com.zerosoft.shuguang.service.MqttSendClient;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import static org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.getEnvironment;

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class ShuguangApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ShuguangApplication.class, args);
    }

    @Resource
    private SerialPortProperties serialPortProperties;

    @Resource
    private MqttSendClient client;
    @Override
    public void run(String... args) throws Exception {
        try{
//            client.connect();
//            this.client.publish(false, "event/abc", "hello world");
            SerialPortManager.getSerialPortList();
            // win
            SerialPortManager.connectSerialPort(
                    serialPortProperties,
                    client
            );

            // linux centos
            //SerialPortManager.connectSerialPort("ttyS1");
        } catch (Exception e){
            System.out.println(e.toString());
        }
    }

    @PreDestroy
    public void destroy() {
        SerialPortManager.closeSerialPort();
    }
}
